from typing import List
from typing import Any
from dataclasses import dataclass
import json
"""
Модель обработки json пакета приходящего от сервера с ответом на текстовый запрос от пользователя от  GigaChat
"""
@dataclass
class Message:
    content: str
    role: str

    @staticmethod
    def from_dict(obj: Any) -> 'Message':
        _content = str(obj.get("content"))
        _role = str(obj.get("role"))
        return Message(_content, _role)
@dataclass
class Choice:
    message: Message
    index: int
    finish_reason: str

    @staticmethod
    def from_dict(obj: Any) -> 'Choice':
        _message = Message.from_dict(obj.get("message"))
        _index = int(obj.get("index"))
        _finish_reason = str(obj.get("finish_reason"))
        return Choice(_message, _index, _finish_reason)

@dataclass
class Usage:
    prompt_tokens: int
    completion_tokens: int
    total_tokens: int
    system_tokens: int

    @staticmethod
    def from_dict(obj: Any) -> 'Usage':
        _prompt_tokens = int(obj.get("prompt_tokens"))
        _completion_tokens = int(obj.get("completion_tokens"))
        _total_tokens = int(obj.get("total_tokens"))
        _system_tokens = int(obj.get("system_tokens"))
        return Usage(_prompt_tokens, _completion_tokens, _total_tokens, _system_tokens)

@dataclass
class Root:
    choices: List[Choice]
    created: int
    model: str
    object: str
    usage: Usage

    @staticmethod
    def from_dict(obj: Any) -> 'Root':
        _choices = [Choice.from_dict(y) for y in obj.get("choices")]
        _created = int(obj.get("created"))
        _model = str(obj.get("model"))
        _object = str(obj.get("object"))
        _usage = Usage.from_dict(obj.get("usage"))
        return Root(_choices, _created, _model, _object, _usage)

# Example Usage
# jsonstring = json.loads(myjsonstring)
# root = Root.from_dict(jsonstring)
